package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.dto.AppraisalRecordsDto;
import com.woniuxy.goods.dto.ValueRecordDto;
import com.woniuxy.goods.entity.GoodsAppraisalRecords;
import com.woniuxy.goods.entity.GoodsValueRecords;
import com.woniuxy.goods.form.AddAppraisalForm;
import com.woniuxy.goods.form.AddValueForm;
import com.woniuxy.goods.form.FindRecordForm;
import com.woniuxy.goods.param.AddAppraisalParam;
import com.woniuxy.goods.param.AddValueParam;
import com.woniuxy.goods.param.FindRecordParam;
import com.woniuxy.goods.service.GoodsAppraisalRecordsService;
import com.woniuxy.goods.service.GoodsValueRecordsService;
import com.woniuxy.goods.util.TokenUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 评估记录管理控制层接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "评估记录管理控制层接口")
@RequestMapping("/goodsValueRecords")
public class GoodsValueRecordsController {

    /**
     * 评估记录管理服务层接口
     */
    @Resource
    private GoodsValueRecordsService valueService;
    /**
     * 解析token工具类
     */
    @Resource
    private TokenUtil tokenUtil;

    /**
     * 新增评估记录
     * @param form 新增评估记录对象
     * @param token 携带当前登录管理员id的请求头
     * @return 评估记录
     * @throws Exception
     */
    @ApiOperation("新增评估记录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsValueRecords.class)
    })
    @PostMapping("/addValue")
    @Permission(roles = {"评估师"})
    public Result addValue(@Validated @RequestBody AddValueForm form, BindingResult br, @RequestHeader String token) throws Exception{
        if(br.hasErrors())throw  new ParamException(10001,"参数有误");
        System.out.println(form);
        Integer accountId = tokenUtil.getAccountId(token);
        AddValueParam param = BeanUtil.toBean(form, AddValueParam.class);
        param.setAccountId(accountId);
        GoodsValueRecords records = valueService.addValue(param);
        return Result.success("新增评估记录成功",records);
    }

    /**
     * 根据物品id查询历史评估记录
     * @param goodsId 物品id
     * @return 评估记录
     * @throws Exception
     */
    @ApiOperation("根据物品id查询历史评估记录")
    @ApiImplicitParam("物品id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsValueRecords.class)
    })
    @GetMapping("/findValueByGoodsId")
    public Result findValueByGoodsId(Integer goodsId) throws Exception{
        List<GoodsValueRecords> records = valueService.findValueByGoodsId(goodsId);
        return Result.success("根据物品id查询历史评估记录成功",records);
    }

    /**
     * 根据物品id查询最新评估记录及历史评估记录
     * @param goodsId 物品id
     * @return 最新评估记录及历史评估记录
     * @throws Exception
     */
    @ApiOperation("根据物品id查询最新评估记录及历史评估记录")
    @ApiImplicitParam("物品id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsValueRecords.class)
    })
    @GetMapping("/findNewValueByGoodsId")
    public Result findNewValueByGoodsId(Integer goodsId) throws Exception{
        ValueRecordDto records = valueService.findNewValueByGoodsId(goodsId);
        return Result.success("根据物品id查询最新评估记录及历史评估记录成功",records);
    }

    /**
     * 查询所有最新的评估记录及历史记录
     * @param form 查询记录参数对象
     * @return 评估记录集合
     * @throws Exception
     */
    @ApiOperation("查询所有最新的评估记录及历史记录")
    @ApiImplicitParam("查询记录参数对象")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsAppraisalRecords.class)
    })
    @PostMapping("/findNewValues")
    public Result findNewValues(@RequestBody FindRecordForm form) throws Exception{
        IPage<ValueRecordDto> records = valueService.findNewValues(BeanUtil.toBean(form, FindRecordParam.class));
        return Result.success("查询所有最新的评估记录及历史记录成功",records);
    }

}

